package com.mlnx.web.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.form.PageForm;
import com.mlnx.device.entity.Device;
import com.mlnx.device.pojo.dto.SpoDeviceDto;
import com.mlnx.device.pojo.form.SpoDeviceFilterForm;
import com.mlnx.device.pojo.form.SpoDeviceForm;
import com.mlnx.device.service.DeviceService;
import com.mlnx.device.service.SpoDeviceService;
import com.mlnx.service.common.enums.ResponseEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


@Api(value = "血氧设备接口", tags = {"血氧设备接口"})
@RestController
@RequestMapping("device/spo")
public class SpoDeviceController extends BaseController {

    @Autowired
    private SpoDeviceService spoDeviceService;

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "添加血氧设备")
    @PostMapping(value = "")
    public Response addSpoDevice(@Valid SpoDeviceForm spoDeviceForm, HttpServletRequest httpServletRequest) {
        Device device = deviceService.getOne(new QueryWrapper<Device>().eq("numeration", spoDeviceForm.getNumeration()));
        if (device != null)
            if (device.getStatus() == 1)
                deviceService.removeById(device.getId());
            else
                return result(ResponseEnum.DeviceExisted);
        Integer userId = Integer.parseInt((String) httpServletRequest.getAttribute("username"));
        spoDeviceService.addSpoDevice(spoDeviceForm,userId);

        return result();
    }


    @ApiOperation(value = "分页获取血氧设备", notes = "")
    @GetMapping(value = "")
    public ResponseData<Page<SpoDeviceDto>> list(@Valid PageForm pageForm, SpoDeviceFilterForm spoDeviceFilterForm
            , HttpServletRequest httpServletRequest) {
        Integer userId = Integer.parseInt((String) httpServletRequest.getAttribute("username"));
        if(userId != 1){
            spoDeviceFilterForm.setUserId(userId);
        }
        return result(spoDeviceService.list(pageForm, spoDeviceFilterForm));
    }

    //无用
    @ApiOperation(value = "获取在线血氧设备", notes = "")
    @GetMapping(value = "/online")
    public ResponseData<List<SpoDeviceDto>> listOnlines(SpoDeviceFilterForm spoDeviceFilterForm) {

        return result(spoDeviceService.listOnlines(spoDeviceFilterForm));
    }

    @ApiOperation(value = "批量删除血氧设备", notes = "")
    @DeleteMapping(value = "")
    public Response deleteSpoDevices(Integer[] ids) {

        deviceService.deleteDevices(ids);

        return result();
    }

    @ApiOperation(value = "修改血氧设备信息", notes = "")
    @PutMapping(value = "/{id}")
    public Response editSpoDevice(@PathVariable("id") Integer id, @Valid SpoDeviceForm spoDeviceForm) {

        spoDeviceService.updateSpoDevice(id, spoDeviceForm);

        return result();
    }
}
